import { Component, OnInit, Input } from '@angular/core';
import { ApiService } from "@core/services/http/api.service";
import { GridConfigService } from 'app/core/config/grid-config.service';
import { GridOptions } from '@ag-grid-community/core';
import {Events} from "@core/services/events/Events";
import {ApesMessageService} from "@shared/apes/message";
import {DatePipe} from "@angular/common";

@Component({
  selector: 'car-table',
  templateUrl: './car-table.component.html',
  styleUrls: ['./car-table.component.scss']
})
export class CarTableComponent implements OnInit {
  @Input()
  set parmas(value:any) {
    this.title = value["title"];
    this.titleList = value["titleList"];
    this.steps = value["stepsApi"];
    this._parmas = value;
  }

  get parmas() {
    return this._parmas;
  }

  _parmas:any;
  title:string;
  titleList:any[] = [];
  steps:string;
  itmeData = "0分钟";
  itemList:any[] = [];

  jjcdhNum:any;
  check:string = '1';
  cphm:any;
  mdid: any;

  /** Grid 相关 **/
  rowData:any[] = [];
  resultData:any;
  gridOptions:GridOptions;
  pinnedBottomRowData:any[] = [];
  domLayout: any;
  rowClassRules :any;

  constructor(private apiService:ApiService,
              private gridConfigService:GridConfigService,
              private event:Events,
              private msg: ApesMessageService,
              private datePipe: DatePipe) {
    this.titleList;

    // 初始化 grid
    this.gridOptions = <GridOptions>{
      toolPanelSuppressSideButtons: true,
      suppressExcelExport: true,
      suppressCsvExport: true,
      showToolPanel: false,
      rowHeight: 35,
    };
    this.gridOptions.localeText = gridConfigService.getLocaleText();
    this.domLayout = 'autoHeight';
    this.rowClassRules = {
      "grid-tab-warning": 'data.TIO > 0'
    };

  }

  ngOnInit() {
  }

  getFun(key: string,cphm: string, mdid: string, checkKeys: string) {
    this.jjcdhNum = key;
    this.cphm = cphm;
    this.mdid = mdid;
    this.check = checkKeys;
    this.rowData = [];
    this.itemList = [];
    this.itmeData = '0分钟';
    this.getHead();
    this.getSteps();
    this.getData();
  }

  getHead() {
    if (this.parmas["headApi"]) {
      let data = {'interfaceMethod': this.parmas["headApi"], 'pdata': {'JJCDH': this.jjcdhNum,'MDID': this.mdid, 'CPHM': this.cphm, 'ZT': this.check}};
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              for (let i = 0, len = data.length; i < len; i++) {
                if (data[i].ZHS !== undefined) {
                  this.itmeData = this.toHour(data[i].ZHS);
                } else {
                  this.itmeData = "0分钟";
                }
              }
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  getSteps() {
    if (this.parmas["stepsApi"]) {
      let data = {'interfaceMethod': this.parmas["stepsApi"], 'pdata': {'JJCDH': this.jjcdhNum, 'MDID': this.mdid, 'CPHM': this.cphm,'ZT': this.check}};
      // debugger;
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          //debugger;
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              for (let i = 0, len = data.items.length; i < len; i++) {
                data.items[i]['Description'] = this.toHour(data.items[i]['Description']);
              }
              this.itemList.push(data);
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  getData() {
    if (this.parmas["api"]) {
      let data = {'interfaceMethod': this.parmas["api"], 'pdata': {'JJCDH': this.jjcdhNum, 'MDID': this.mdid, 'CPHM': this.cphm, 'ZT': this.check}};
      this.apiService.post('report.interface', {'body': JSON.stringify(data)}).subscribe(
        (resultData) => {
          const result = JSON.parse(resultData);
          if (result.success) {
            if (result.obj.length !== 0) {
              const data = JSON.parse(result.obj);
              for (let i = 0, len = data.length; i < len; i++) {

                data[i]['SGKSRQ_SJ'] = this.datePipe.transform(
                  data[i]['SGKSRQ_SJ'],'yyyy-MM-dd HH:mm:ss');
                data[i]['SGJSRQ_SJ'] = this.datePipe.transform(
                  data[i]['SGJSRQ_SJ'],'yyyy-MM-dd HH:mm:ss');

                if (data[i]['WCBJ'] === 1) {
                  data[i]['WCBJ'] = '√';
                } else {
                  data[i]['WCBJ'] = '×';
                }
              }
              if (data.DETAILS == undefined) {
                this.rowData = data;
              } else {
                this.rowData = data.DETAILS;
              }
            }
          } else {
            this.msg.info(result.msg);
          }
        });
    }
  }

  toHour(minutes) {
    if (Math.floor(minutes / 60 / 24) >= 1) {
      if (Math.floor(minutes / 60 / 24) < 10) {
        return (Math.floor(minutes / 60 / 24) + '天' + (minutes % 60 % 24) + '小时');
      }
      return (Math.floor(minutes / 60 / 24) + '天');
    } else if (Math.floor(minutes / 60 / 24) < 1) {
      if (Math.floor(minutes) < 60) {
        return (Math.floor(minutes % 60) + '分钟');
      }
      if(Math.floor(minutes % 60) === 0){
        return (Math.floor(minutes / 60) + '小时')
      }
      return (Math.floor(minutes / 60) + '小时' + (minutes % 60) + '分钟');
    }
  }

}
